package com.example.ggpicturegg.bottom;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ggpicturegg.Mysql_com.ImageDao;
import com.example.ggpicturegg.Mysql_com.ImageInfo;
import com.example.ggpicturegg.R;

import com.example.ggpicturegg.first.Like_list;
import com.example.ggpicturegg.first.RecyclerviewActivity;
import com.example.ggpicturegg.myAdapter.MyAdapter;
import com.example.ggpicturegg.tool.CommonUtils;
import com.example.ggpicturegg.tool.Loaddata;

import java.util.List;

public class Friend_b extends AppCompatActivity {

    private  TextView frist;
    private  TextView talk;
    private  TextView myinfo;
    private  EditText tv_search;
    private  Button btn_search;
    private ImageView im_add_x;

    private RecyclerView mRvGrid;

    private List<ImageInfo> image_list;       // 照片集合
    private ImageDao imageDao;  // image 操作实列
    private Handler mainHandler;
    private MyAdapter myAdapter;

    private Bundle bundle1;
    private String sss=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_b);

        initView();
        transinfo();
        loadImagedb();


    }

    private void transinfo(){
        bundle1 = getIntent().getExtras();
        sss = bundle1.getString("Re_to_friend");
        if(sss!=null){Log.d("Friend 接受 Re 的name", "    ---------- is ---------    "+ sss);}
        if(sss==null){
            sss = bundle1.getString("myinfo_to_friend");
            if(sss!=null){Log.d("Friend 接受 Myinfo 的name", "    ---------- is ---------    "+ sss);}
        }
        if(sss==null){
            sss = bundle1.getString("talk_to_friend");
            if(sss!=null){Log.d("Friend 接受 talk 的name", "    ---------- is ---------    "+ sss);}
        }
        if(sss==null){
            sss = bundle1.getString("like_to_friend");
            if(sss!=null){Log.d("Friend 接受 like 的name", "    ---------- is ---------    "+ sss);}
        }
        if(sss==null){
            sss = bundle1.getString("care_to_friend");
            if(sss!=null){Log.d("Friend 接受 care 的name", "    ---------- is ---------    "+ sss);}
        }
    }
    private void initView(){
        im_add_x = findViewById(R.id.add_x);
        frist = findViewById(R.id.frist_x);
        talk = findViewById(R.id.talk_x);
        myinfo = findViewById(R.id.myinfo_x);
        tv_search = findViewById(R.id.tv_search);
        btn_search = findViewById(R.id.btn_search);
        OnClick onClick = new OnClick();
        frist.setOnClickListener(onClick);
        talk.setOnClickListener(onClick);
        myinfo.setOnClickListener(onClick);
        btn_search.setOnClickListener(onClick);
        im_add_x.setOnClickListener(onClick);
        mRvGrid = (RecyclerView)findViewById(R.id.rv_grid);
        mRvGrid.setLayoutManager(new GridLayoutManager(Friend_b.this,2));

        imageDao = new ImageDao();
        mainHandler = new Handler(getMainLooper()) ; //获取主线程
    }

    //加载数据
    private void loadImagedb(){
        new Thread(new Runnable() {
            @Override
            public void run() {

                image_list = imageDao.getUserFriendList(sss);  //获取全部用户数据

                //image_list = imageDao.getAllUserList();  //获取全部用户数据

                for(int i=0;i<image_list.size();i++){
                    Bitmap bitmap =  Loaddata.getImage(image_list.get(i).getPicture_name(),image_list.get(i).getId());
                 //   System.out.println("------------AAAAA   " + i +"   AAAAAA-------------" );
                    int finalI = i;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            image_list.get(finalI).setBitmap(bitmap);
                            showListViewData();
                        }
                    });
                }

            }
        }).start();
    }

    //显示数据
    private void showListViewData(){
        if(myAdapter==null){
            myAdapter = new MyAdapter(Friend_b.this, image_list, new MyAdapter.OnItemClickListener() {
                @Override
                public void OnClick(int pos) {
                    Toast.makeText(Friend_b.this,"click"+pos,Toast.LENGTH_SHORT).show();
                }
            },sss);
            mRvGrid.setAdapter(myAdapter);
        }
        else{
            myAdapter.setImage_list(image_list);
            myAdapter.notifyDataSetChanged();
        }
    }

     // 查找数据
    private void SearchFriendImage(){
        final String name = tv_search.getText().toString().trim();
        new Thread(new Runnable() {
            @Override
            public void run() {

                image_list = imageDao.getOneUserList(name);  //获取全部用户数据
                if(image_list.size()==0){
                    Looper.prepare();
                    Toast.makeText(Friend_b.this, "空空如也！！！", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
                for(int i=0;i<image_list.size();i++){
                    Bitmap bitmap =  Loaddata.getImage(image_list.get(i).getPicture_name(),image_list.get(i).getId());
                   // System.out.println("-----123---------- "+image_list.get(i).getPicture_name()+" -------------------------");

                    int finalI = i;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            image_list.get(finalI).setBitmap(bitmap);
                            showListViewData();
                        }
                    });
                }

            }
        }).start();
    }

    class OnClick implements View.OnClickListener {
        private Intent intent;
        private Bundle bundle;

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.frist_x:
                    intent = new Intent(Friend_b.this, RecyclerviewActivity.class);
                    Log.d("friend 发送 Re 的name", "    ---------- is ---------    "+ sss);
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("friend_to_Re",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.myinfo_x:
                    intent = new Intent(Friend_b.this, Myinfo.class);
                    Log.d("friend 发送 myinfo 的name", "    ---------- is ---------    "+ sss);
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("friend_to_myinfo",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.talk_x:
                    intent = new Intent(Friend_b.this, Talk_b.class);
                    Log.d("friend 发送 talk 的name", "    ---------- is ---------    "+ sss);
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("friend_to_talk",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.btn_search:
                    SearchFriendImage();
                    break;
                case R.id.add_x:
                    intent = new Intent(Friend_b.this, Publish_b.class);
                    Log.d("friend 发送 publish 的name", "    ---------- is ---------    "+ sss);
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("friend_to_add",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;

            }
        }
    }
}